package com.jyniubi.gmail.pms.service.impl;

import com.jyniubi.gmail.pms.entity.*;
import com.jyniubi.gmail.pms.feign.GmallSmsClient;
import com.jyniubi.gmail.pms.mapper.SkuMapper;
import com.jyniubi.gmail.pms.mapper.SpuDescMapper;
import com.jyniubi.gmail.pms.service.*;
import com.jyniubi.gmail.pms.vo.SkuVo;
import com.jyniubi.gmail.pms.vo.SpuAttrValueVo;
import com.jyniubi.gmail.pms.vo.SpuVo;
import com.jyniubi.gmail.sms.vo.SkuSalesVo;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jyniubi.gmail.common.bean.PageResultVo;
import com.jyniubi.gmail.common.bean.PageParamVo;

import com.jyniubi.gmail.pms.mapper.SpuMapper;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service("spuService")
public class SpuServiceImpl extends ServiceImpl<SpuMapper, SpuEntity> implements SpuService {

    @Resource
    private RabbitTemplate rabbitTemplate;
    @Resource
    private SpuDescService spuDescService;
    @Resource
    private SpuAttrValueService spuAttrValueService;
    @Resource
    private SkuMapper skuMapper;
    @Resource
    private SkuImagesService skuImagesService;
    @Resource
    private SkuAttrValueService skuAttrValueService;
    @Resource
    private GmallSmsClient gmallSmsClient;

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<SpuEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<SpuEntity>()
        );

        return new PageResultVo(page);
    }

    @Override
    public PageResultVo querySpuByIdOrName(Long categoryId, PageParamVo paramVo) {
        QueryWrapper<SpuEntity> wrapper = new QueryWrapper<SpuEntity>();
        if (categoryId != 0) {
            wrapper.eq("category_id", categoryId);
        }
        String key = paramVo.getKey();
        if (StringUtils.isNotBlank(key)) {
            wrapper.and(t -> t.eq("id", key).or().like("name", key));
        }
        IPage<SpuEntity> page = this.page(
                paramVo.getPage(),
                wrapper
        );
        return new PageResultVo(page);
    }

    @Override
    @GlobalTransactional
    public void bigSave(SpuVo spu) {
        //1.保存spu相关的表信息
        //1.1保存pms_spu
        Long spuId = saveSpuInfo(spu);
        //1.2保存pms_spu_desc
        this.spuDescService.saveSpuDesc(spu, spuId);
        //1.3保存pms_spu_attr_value
        this.spuAttrValueService.saveBaseAttrValue(spu, spuId);
        //2.保存sku相关的表信息
        saveSkuInfo(spu, spuId);
//        int i = 1/0;
        this.rabbitTemplate.convertAndSend("PMS.ITEM.EXCHANGE","item.insert",spuId);
    }

    private void saveSkuInfo(SpuVo spu, Long spuId) {
        List<SkuVo> skus = spu.getSkus();
        if (CollectionUtils.isEmpty(skus)) {
            return;
        }
        skus.forEach(skuVo -> {
            //2.1保存pms_sku
            skuVo.setBrandId(spu.getBrandId());
            skuVo.setCategoryId(spu.getCategoryId());
            skuVo.setSpuId(spuId);
            List<String> images = skuVo.getImages();
            if (CollectionUtils.isEmpty(images)) {
                skuVo.setDefaultImage(StringUtils.isBlank(skuVo.getDefaultImage()) ? images.get(0) : skuVo.getDefaultImage());
            }
            this.skuMapper.insert(skuVo);
            Long skuId = skuVo.getId();
            //2.2保存pms_sku_images
            if (!CollectionUtils.isEmpty(images)) {
                this.skuImagesService.saveBatch(images.stream().map(image -> {
                    SkuImagesEntity skuImagesEntity = new SkuImagesEntity();
                    skuImagesEntity.setSkuId(skuId);
                    skuImagesEntity.setUrl(image);
                    skuImagesEntity.setDefaultStatus(StringUtils.equals(skuVo.getDefaultImage(), image) ? 1 : 0);
                    return skuImagesEntity;
                }).collect(Collectors.toList()));
            }
            //2.3保存pms_sku_attr_value
            List<SkuAttrValueEntity> saleAttrs = skuVo.getSaleAttrs();
            saleAttrs.forEach(skuAttrValueEntity -> {
                skuAttrValueEntity.setSkuId(skuId);
            });
            this.skuAttrValueService.saveBatch(saleAttrs);
            //3.保存营销sms相关的表信息
            //3.1保存sms_sku_bounds
            //3.2保存sms_sku_full_reduction
            //3.3保存sms_sku_ladder
            SkuSalesVo skuSalesVo = new SkuSalesVo();
            BeanUtils.copyProperties(skuVo, skuSalesVo);
            skuSalesVo.setSkuId(skuId);
            this.gmallSmsClient.saveSales(skuSalesVo);
        });
    }

    /*private void saveBaseAttrValue(SpuVo spu, Long spuId) {
        List<SpuAttrValueVo> baseAttrs = spu.getBaseAttrs();
        if (!CollectionUtils.isEmpty(baseAttrs)){
            this.spuAttrValueService.saveBatch(baseAttrs.stream().map(spuAttrValueVo -> {
                SpuAttrValueEntity spuAttrValueEntity = new SpuAttrValueEntity();
                BeanUtils.copyProperties(spuAttrValueVo,spuAttrValueEntity);
                spuAttrValueEntity.setSpuId(spuId);
                return spuAttrValueEntity;
            }).collect(Collectors.toList()));
        }
    }*/

    /*private void saveSpuDesc(SpuVo spu, Long spuId) {
        if (!CollectionUtils.isEmpty(spu.getSpuImages())){
            SpuDescEntity spuDescEntity = new SpuDescEntity();
            spuDescEntity.setDecript(StringUtils.join(spu.getSpuImages(),","));
            spuDescEntity.setSpuId(spuId);
            this.spuDescMapper.insert(spuDescEntity);
        }
    }*/

    private Long saveSpuInfo(SpuVo spu) {
        spu.setCreateTime(new Date());
        spu.setUpdateTime(spu.getCreateTime());
        this.save(spu);
        return spu.getId();
    }
}